import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/model/note_model.dart';
import 'package:flutter_douban/tool_views/stance_page.dart';
import 'package:video_player/video_player.dart';

class VideoPage extends StatefulWidget {
  final String video_url;
  final String video_cover_height
  ;
  final String video_cover_width;

  const VideoPage({Key? key, required this.video_url,required this.video_cover_height,required this.video_cover_width}) : super(key: key);

  @override
  _VideoPageState createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  late VideoPlayerController _controller;
  ///视频播放比
  var dura;

  @override
  void initState() {
    super.initState();

    _controller = VideoPlayerController.network(widget.video_url)
      ..initialize().then((a) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {
          _controller.play();
        });
      });
    _controller.setLooping(true);

    //执行监听，只要有内容就会刷新
    _controller.addListener(() {
      setState(() {
        //进度条的播放进度，用当前播放时间除视频总长度得到
        dura = _controller.value.position.inSeconds /
            _controller.value.duration.inSeconds;
        // print('哈哈哈哈' + _controller.value.position.inSeconds.toString());
        // print('嘿嘿嘿嘿' + _controller.value.duration.inSeconds.toString());
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    var isPlay = _controller.value.isPlaying;
    /// 是否显示视频控制层
    var isShowControll;

    List<Widget> widgetList = [];

    widgetList.add(Container(
        color: Colors.black,
        width: MediaQuery.of(context).size.width,
        height: int.parse(widget.video_cover_height) *
            MediaQuery.of(context).size.width /
            int.parse(widget.video_cover_width),
        child: GestureDetector(
            onTap: () {
              setState(() {
                _controller.value.isPlaying
                    ? _controller.pause()
                    : _controller.play();
                // isShowControll = !isShowControll;
              });
            },
            child: _controller.value.isInitialized
                ? AspectRatio(
                    aspectRatio: _controller.value.aspectRatio,
                    child: VideoPlayer(_controller),
                  )
                : StancePage())));

    if (_controller.value.isInitialized && !isPlay) {
      widgetList.add(
              InkWell(
            child: Image.asset(
              'assets/images/common/player_play.png',
              width: 60,
              height: 60,
            ),
            onTap: () {
              setState(() {
                _controller.value.isPlaying
                    ? _controller.pause()
                    : _controller.play();
              });
            },
          )
      );
    }

    widgetList.add(
      //视频进度条
      Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: LinearProgressIndicator(
            backgroundColor: Colors.grey,
            value: dura,
            valueColor: new AlwaysStoppedAnimation<Color>(AppColors.yellow),
          )),
    );

    return Stack(
      alignment: Alignment.center,
      children: widgetList,
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}
